#include <fstream>
#include<vector>
using namespace std;

vector<char> carved;
vector<char> color;
int n;
ifstream in("in.txt");
ofstream out("out.txt");



void dfs(int s) {

    color[s] = 1;
    out << s/n << s%n << '\n';
    if (s/n < n-1 && !carved[s+n] && !color[s+n]) dfs(s+n);
    if (s/n > 0 && !carved[s-n] && !color[s-n]) dfs(s-n);
    if (s%n > 0 && !carved[s-1] && !color[s-1]) dfs(s-1);
    if (s%n < n-1 && !carved[s+1] && !color[s+1]) dfs(s+1);


}


int main() {

    in >> n;

    carved.resize(n*n);
    color.resize(n*n);

    while (!in.eof()) {
        int r,c;
        in >> r >> c;
        carved[r*n + c] = 1;
    }

    int count = 0;
    for (int i=0; i<n*n; i++) {
        if (!color[i] && !carved[i]) {
            count++;
            dfs(i);
        }

    }

    out << '\n' <<count;



}
